<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Request;
use app\admin\model\ConfigModel;

class Config extends Common
{   
   
  public function form(){
    $ConfigModel=new ConfigModel;
    $data=$ConfigModel->get(1);
    if(request()->isPost()){
        $data=input('post.');
        unset($data['image']);
        if($ConfigModel->save($data,['id'=>'1'])){
            return  $this->error('保存成功','/admin/config/form');
        }
        return  $this->error('保存成功','/admin/config/form');
    }
    $this->assign('data',$data);
    return $this->fetch();
  }
    //文件上传
    public function upload(){
      $file = request()->file('image');
      $path='system';
      return json_decode($this->uploads($file,$path),true);
    }
    //文件上传
    public function uploads($file,$path){
        $return=[];
            // 获取表单上传文件 例如上传了001.jpg
            // $file = request()->file('image');
            // 移动到框架应用根目录/public/uploads/ 目录下
            if($file){
                // $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'.DS.'admin');
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'.DS.$path);
                if($info){
                    // 成功上传后 获取上传信息
                    $return['code']=0;
                    $return['msg']="上传成功";
                    $return['data']='/uploads/system/'.str_replace('\\','/',$info->getSaveName());
                }else{
                    // 上传失败获取错误信息
                    $return['code']=1;
                    // $return['msg']="上传成功";
                    $return['msg']="上传失败";
                    $return['data']=$file->getError();
                }
            }
        return json_encode($return);
    }
}
